package com.lwc.backendsinglestructure.queue.producer;


import com.lwc.backendsinglestructure.exception.BusinessException;
import com.lwc.backendsinglestructure.queue.enums.MessageBroadChannelEnum;
import com.lwc.backendsinglestructure.queue.enums.MessageQueueEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

//@RefreshScope
@Service
public class MessageQueueProducer<T, P> {

//    @Autowired
//    private RocketMessageQueueSender<T, P> rocketMessageQueueSender;

    @Autowired
    private RedisMessageQueueSender<T, P> redisMessageQueueSender;

    /**
     * 消息队列开关配置，支持：rocketmq、redis、kafka等
     */
    @Value("${message-queue.used:redis}")
    private String messageQueueUsed;

    /**
     * 普通队列消息发送
     *
     * @param queue
     * @param message
     */
    public void send(MessageQueueEnum queue, T message) {

        this.getSender().send(queue, message);
    }

    /**
     * 优先级队列消息发送
     *
     * @param queue
     * @param message
     * @param priority
     */
    public boolean prioritySend(MessageQueueEnum queue, T message, P priority) {

        return this.getSender().prioritySend(queue, message, priority);
    }

    /**
     * 广播消息发送
     *
     * @param channel
     * @param message
     */
    public void broadSend(MessageBroadChannelEnum channel, T message) {

        this.getSender().broadSend(channel, message);
    }

    private AbstractMessageQueueSender<T, P> getSender() {
        // TODO 消息队列类型判断
//        if (messageQueueUsed.equals("rocketmq")) {
//            return rocketMessageQueueSender;
//        } else if (messageQueueUsed.equals("redis")) {
//            return redisMessageQueueSender;
//        }
//        throw new BusinessException("不支持的队列类型！");

        // 当前默认支持 Redis的消息队列发送
        return redisMessageQueueSender;

    }
}
